﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 库房标签打印
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string IP;
        string database;
        string uid;
        string pwd;
        string SHDW;
        string supply;
        SqlConnection conn;
        BarTender.Application btapp;
        BarTender.Format btFormat;        
        private void Form1_Load(object sender, EventArgs e)
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict = ConfigHelper.ReadAll(@"setting.config");
            foreach (var v in dict)
            {
                if (v.Key == "IP")
                {
                    IP = v.Value;
                }
                if (v.Key == "database")
                {
                    database = v.Value;
                }
                if (v.Key == "uid")
                {
                    uid = v.Value;
                }
                if (v.Key == "pwd")
                {
                    pwd = v.Value;
                }
                if (v.Key == "SHDW")
                {
                    SHDW = v.Value;
                }
                if (v.Key == "supply")
                {
                    supply = v.Value;
                }
            }
            textBox1.Text = supply;
            textBox2.Text = SHDW;
            try
            {
                conn = new SqlConnection("server='" + IP + "';initial catalog='" + database + "';uid='" + uid + "';pwd='" + pwd + "'");
                conn.Open();
            }
            catch (Exception ee)
            {
                MessageBox.Show("服务器连接失败");
                Application.Exit();
            }
            btapp = new BarTender.Application();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "" && textBox4.Text !="")
            {
                btFormat = btapp.Formats.Open(System.Environment.CurrentDirectory + "\\打印模板.btw", false, "");
                btFormat.PrintSetup.IdenticalCopiesOfLabel = 1;  //设置同序列打印的份数
                btFormat.SetNamedSubStringValue("SHDW", textBox2.Text ); //向bartender模板传递变量
                btFormat.SetNamedSubStringValue("supply", textBox1.Text ); //向bartender模板传递变量
                btFormat.SetNamedSubStringValue("matName", label8.Text ); //向bartender模板传递变量
                btFormat.SetNamedSubStringValue("matSpec", label9.Text ); //向bartender模板传递变量                        
                btFormat.SetNamedSubStringValue("matCode", label7.Text ); //向bartender模板传递变量
                btFormat.SetNamedSubStringValue("matSN", textBox3.Text ); //向bartender模板传递变量                
                btFormat.SetNamedSubStringValue("qty", textBox4.Text); //向bartender模板传递变量
                btFormat.PrintOut(true, false); //第二个false设置打印时是否跳出打印属性
                btFormat.Close(BarTender.BtSaveOptions.btSaveChanges); //退出时是否保存标签
                textBox3.Text = "";
                textBox4.Text = "";
            }
            else
            {
                MessageBox.Show("请输入完整信息");
            }
        }

        private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                string str1 = "select * from saoma where message='" + textBox3.Text + "'";
                //SqlCommand cmd = new SqlCommand(str, conn);
                SqlDataAdapter sda1 = new SqlDataAdapter(str1,conn);
                DataSet ds1 = new DataSet();
                sda1.Fill(ds1);
                if (ds1.Tables[0].Rows.Count == 0)
                {
                    MessageBox.Show("未查询到此条码");
                    return;
                }
                string cInvCode = ds1.Tables[0].Rows[0]["liaohao"].ToString();
                string str2 = "select * from Inventory where cInvCode='" + cInvCode + "'";
                SqlDataAdapter sda2 = new SqlDataAdapter(str2, conn);
                DataSet ds2 = new DataSet();
                sda2.Fill(ds2);
                if (ds2.Tables[0].Rows.Count == 0)
                {
                    MessageBox.Show("未查询到此料号信息");
                    return;
                }
                label7.Text = ds2.Tables[0].Rows[0]["cInvCode"].ToString();
                label8.Text = ds2.Tables[0].Rows[0]["cInvName"].ToString();
                label9.Text = ds2.Tables[0].Rows[0]["cInvStd"].ToString();
                textBox4.Text = ds1.Tables[0].Rows[0]["shuliang"].ToString();
                //textBox3.Text = "";
                button1.Focus();
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            btapp.Quit(BarTender.BtSaveOptions.btSaveChanges);//界面退出时同步退出bartender进程
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            ConfigHelper.Write(@"setting.config", "supply", textBox1.Text );
            ConfigHelper.Write(@"setting.config", "SHDW", textBox2.Text);
        }
    }
}
